package org.pixelami.twisted.spread.pb
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	
	import org.pixelami.twisted.TwistedSocket;
	import org.pixelami.twisted.utils.StreamTools;
	
	public class Protocol extends EventDispatcher
	{
		private var _transport: TwistedSocket;
		
		public function set transport(s:TwistedSocket):void
		{
			_transport = s;
		}
		
		public function get transport():TwistedSocket
		{
			return _transport;
		}
		
		public function Protocol()
		{
		}
		
		public function closeHandler(event:Event):void{}
		
		//public function connectHandler(event:Event):void{}
		
		//public function socketDataHandler(event:ProgressEvent):void{}
		
		public function connectionLost(reason:Object):void{}
		
		public function connectionMade():void{trace("connected")}
		
		
		
		
		public function dataReceived(obj:*):void{}
		
		public function writeList(obj:Object,ba:ByteArray):void
		{
			var l:int = obj.length;
			if(l>0)
			{
				ba.writeByte(10);
				ba.writeInt(l);
				
				for(var i:String in obj)
				{
					//trace("writeList - "+obj[i]);
					if(obj[i] is Array) writeList(obj[i],ba);
					else if(obj[i] is ByteArray) 
					{
						ba.writeObject(obj[i].readMultiByte(0,'us_ascii'));
					}
					else ba.writeObject(obj[i]);
				}
			}
		}
		
		public function prettyPrint(ba:ByteArray):void
		{
			StreamTools.prettyPrint(ba);
		}
	}
}